package com.aode.music.view.fragment.mainfragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.aode.common.bean.request.Request;
import com.aode.common.bean.request.SearchData;
import com.aode.common.bean.response.User;
import com.aode.common.bean.web.Result;
import com.aode.music.R;
import com.aode.music.adapter.page.ChatPagerAdapter;
import com.aode.music.adapter.page.MePagerAdapter;
import com.aode.music.databinding.FragmentMeBinding;
import com.aode.music.viewmodel.MeViewModel;
import com.aode.playlist.view.activity.HomePlayListActivity;
import com.aode.utils.sharedhelper.SharedHelper;
import com.google.android.material.tabs.TabLayout;

import java.util.Map;


public class MeFragment extends Fragment implements TabLayout.OnTabSelectedListener{
    private FragmentMeBinding meBinding;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private SharedHelper sharedHelper;
    private SearchData searchData = new SearchData();
    private MeViewModel meViewModel;
    private User user;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        meBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_me,container,false);
        meViewModel = ViewModelProviders.of(this).get(MeViewModel.class);
        meBinding.setMeFragmet(MeFragment.this);
        initData();
        initView();
        return meBinding.getRoot();
    }

    private void initData() {
        sharedHelper = new SharedHelper(getContext());
        Map<String,String> data = sharedHelper.read();
        searchData.setSearch(data.get("userId"));
        Log.e("TAG->>>>",searchData.getSearch());
        meViewModel.getUser(searchData);
        meViewModel.userResultMutableLiveData().observe(getViewLifecycleOwner(), userResult -> {
            user = userResult.getData();
            if(user!=null){
                Log.e("TAG",user.getUserAvatar());
                meBinding.setUser(user);
            }
        });
    }

    private void initView() {
        tabLayout = meBinding.meTabLayout;
        viewPager = meBinding.meViewPager;
        tabLayout.addTab(tabLayout.newTab().setText("收藏"));
        tabLayout.addTab(tabLayout.newTab().setText("下载"));
        viewPager.setAdapter(new MePagerAdapter(getChildFragmentManager(),tabLayout.getTabCount()));
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(this);
    }


    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    public void click(View view){
        switch (view.getId()){
            case R.id.iv_me_edit:
                showUserInfo();
                break;
        }
    }

    private void showUserInfo() {
        Intent intent = new Intent();
        intent.setClass(getActivity(), HomePlayListActivity.class);
        Bundle bundle = new Bundle();
        bundle.putParcelable("user",user);
        intent.putExtras(bundle);
        startActivityForResult(intent, Request.ME_TO_USER_EDIT);
    }
}





